package com.auth.manage.trade.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.auth.manage.base.responce.SingleResponce;
import com.auth.manage.common.order.api.request.OrderCreateRequest;
import com.auth.manage.common.order.api.service.OrderFacadeService;
import com.auth.manage.common.product.api.response.ProductInfo;
import com.auth.manage.common.product.api.service.ProductFacadeService;
import com.auth.manage.trade.param.BuyParam;
import com.auth.manage.web.vo.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RequestMapping("trade")
@RestController
@RequiredArgsConstructor
public class TradeController {
    private final ProductFacadeService productFacadeService;
    private final OrderFacadeService orderFacadeService;

    // 下单
    // 支付 商品 交易 订单
    @PostMapping("/buy")
    public Result buy(@Validated @RequestBody BuyParam param){
        // 1.调用商品接口，查询商品信息
        // 商品单价在商品表
        // todo 这里的商品单价在商品表中，能否传商品单价作为param
        SingleResponce<ProductInfo> responce = productFacadeService.getProductInfoByProductId(param.getProductId());
        ProductInfo productInfo = responce.getData();
        if (!responce.getSuccess() || productInfo == null){
            // todo 抛出异常
        }
        // 2.组装请求
        OrderCreateRequest request = new OrderCreateRequest();
        // 2.1 计算商品总价
        BigDecimal orderAmount = productInfo.getPrice().multiply(new BigDecimal(param.getCount()));
        // todo 卖家id的设置
        request.create(StpUtil.getLoginIdAsLong(), 0L, orderAmount, param.getProductId(), productInfo.getCover(), productInfo.getName(), param.getCount(), productInfo.getPrice());
        // 3.发送请求创建订单
//        orderFacadeService.createOrder();
    }

}











